package com.base.system.managedBean;

import java.io.Serializable;
import java.util.List;

import javax.faces.context.FacesContext;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.operamasks.faces.annotation.Action;
import org.operamasks.faces.annotation.BeforeRender;
import org.operamasks.faces.annotation.Bind;
import org.operamasks.faces.annotation.DataModel;
import org.operamasks.faces.annotation.EventListener;
import org.operamasks.faces.annotation.Inject;
import org.operamasks.faces.annotation.ManagedBean;
import org.operamasks.faces.annotation.ManagedBeanScope;
import org.operamasks.faces.annotation.SaveState;
import org.operamasks.faces.component.grid.impl.UIDataGrid;
import org.operamasks.faces.user.util.Browser;

import com.base.commons.SpringBeanHolder;
import com.base.security.SecurityConst;
import com.base.security.dao.ISecurityRoleDao;
import com.base.security.entity.SecurityRole;
import com.base.security.service.SecurityEntityManager;

/**
 * 
 * This managed bean is generated automatically
 */
@ManagedBean(name = "system_securityRoleListBean", scope = ManagedBeanScope.REQUEST)
public class SecurityRoleListBean implements Serializable {
	private final static Log logger = LogFactory
			.getLog(SecurityRoleListBean.class);
	private SecurityEntityManager securityEntityManager = SpringBeanHolder
			.getBean("securityEntityManager");
	private ISecurityRoleDao securityRoleDao = SpringBeanHolder
			.getBean("securityRoleDao");
	@Inject
	private FacesContext context;
	@SaveState
	@Bind(id = "roleDisplayName", attribute = "value")
	private String roleDisplayName;

	@Bind(id = "securityRoleGrid", attribute = "binding")
	private UIDataGrid securityRoleGrid;

	@DataModel(id = "securityRoleGrid")
	private List<SecurityRole> getRoles() {
		if (roleDisplayName == null || "".equals(roleDisplayName))
			return securityRoleDao.getAll();
		return securityEntityManager.getRoleByDisplayName(roleDisplayName);
	}

	@BeforeRender
	public void beforeRender(boolean isPostback) {
		logger.debug("Listbean 中的 postBack:" + isPostback);
		if (!isPostback) {
			// data = securityRoleDao.getAll();
			roleDisplayName = null;
		}
	}

	@Action(id = "query")
	public void query() {
		// data = securityEntityManager.getRoleByDisplayName(roleDisplayName);
		securityRoleGrid.reload();
	}

	@Action(id = "reset")
	public void reset() {
		roleDisplayName = null;
		// data = securityRoleDao.getAll();
		securityRoleGrid.reload();
	}

	private boolean initSelected() {
		Object[] objs = securityRoleGrid.getSelectedValues();
		// System.out.println("取得的选取值是：" + objs);
		if (objs == null || objs.length == 0) {
			alertNoRecordSelected();
			return false;
		}
		context.getExternalContext().getSessionMap().put(
				SecurityConst.SECURITYROLE_SELECT_ROW_DATA,
				(SecurityRole) objs[0]);
		return true;
	}

	private void alertNoRecordSelected() {
		Browser.execClientScript("window.alert('请选择一条记录！');");
	}

	public void showSecurityRole() {
		if (initSelected()) {
			String url = "/modules/system/showSecurityRole.faces";
			url = context.getExternalContext().getRequestContextPath() + url;
			String script = "addCenterLayoutTab('"
					+ SecurityConst.SECURITYROLE_SHOW_TAB + "','" + "查看角色"
					+ "', '" + url + "')";
			Browser.execClientScript("window.top", script);
		}
	}

	@Action(id = "editSecurityRole")
	public void editSecurityRole() {
		if (initSelected()) {
			Object[] objs = securityRoleGrid.getSelectedValues();
			SecurityRole sr = (SecurityRole) objs[0];
			if (sr == null)
				return;
			if (SecurityConst.ROLETYPE_SYSTEM.equals(sr.getRoleType())) {
				Browser.execClientScript("window.alert('系统级角色不能编辑！');");
				return;
			}
			String url = "/modules/system/editSecurityRole.faces";
			url = context.getExternalContext().getRequestContextPath() + url;
			String script = "addCenterLayoutTab('"
					+ SecurityConst.SECURITYROLE_EDIT_TAB + "','" + "编辑角色"
					+ "', '" + url + "')";
			Browser.execClientScript("window.top", script);
		}
	}

	@Action(id = "deleteSecurityRole")
	public void deleteSecurityRole() {
		Object[] objs = securityRoleGrid.getSelectedValues();
		if (objs == null || objs.length == 0) {
			return;
		}
		SecurityRole sr = (SecurityRole) objs[0];
		if (sr == null)
			return;
		if (SecurityConst.ROLETYPE_SYSTEM.equals(sr.getRoleType())) {
			Browser.execClientScript("window.alert('系统级角色不能删除！');");
			return;
		}
		securityRoleDao.delete(sr);
		// data.remove((SecurityRole) objs[0]);
		securityRoleGrid.reload();
		// Browser.execClientScript("window.alert('删除成功！');");
	}

	@EventListener("deleteSecurityRole")
	private void deleteSecurityRoleListener(SecurityRole securityRole) {
		// data.remove(securityRole);
	}

	@EventListener("addSecurityRole")
	private void addSecurityRoleListener(SecurityRole securityRole) {
		logger.debug("执行了几次呢？");
		// data.add(securityRole);
	}

}
